package com.lianyou.chapter03.parameter;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.executable.ExecutableValidator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

public class Car {

    static ExecutableValidator executableValidator;
    @BeforeAll
    public static void setUpValidator() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        executableValidator = factory.getValidator().forExecutables();
    }

    @Test
    public void returnValueConstraintsAddUp() throws Exception {
        RentalStation object = new RentalStation(null);
        Method method = RentalStation.class.getMethod( "rentCar" ,Customer.class, Date.class,int.class);

        Set<ConstraintViolation<RentalStation>> violations = executableValidator.validateParameters(
                object,
                method, new Object[]{null,null,null});

        if (!violations.isEmpty()){
            System.out.println(violations.size());

            Iterator<ConstraintViolation<RentalStation>> iterator = violations.iterator();
            ConstraintViolation<RentalStation> next = iterator.next();
            System.out.println(next.getMessage());
            System.out.println(next.getPropertyPath());
            System.out.println(next.getConstraintDescriptor().getAnnotation().annotationType());


            ConstraintViolation<RentalStation> next1 = iterator.next();
            System.out.println(next1.getMessage());
            System.out.println(next1.getPropertyPath());
            System.out.println(next1.getConstraintDescriptor().getAnnotation().annotationType());
        }
    }
}
